import grails.util.GrailsUtil
import org.leaf.green.b2b.*
import org.leaf.green.shiro.*
import org.leaf.green.admin.*
import org.apache.shiro.crypto.hash.Sha512Hash

class aBootStrap {

    def init = { servletContext ->
        switch(GrailsUtil.environment) {
            case "development":
                configureForDevelopment()
                break
            case "production":
                configureForDevelopment()
                break
        }
    }
    def destroy = {
    }

    def configureForDevelopment = {
        /*init market type*/
        def mt_retailer = new MarketType(serialNumber : 'MT1000', name : '零售市场', active : true).save(flush:true)
        def mt_other = new MarketType(serialNumber : 'MT2000', name : '批发市场', active : true).save(flush:true)

        /*init region*/
        def region_jq = new Region(serialNumber : 'REG100', name : '江桥地区', active:true).save(flush:true)
        def region_fz = new Region(serialNumber : 'REG200', name : '丰庄地区', active:true).save(flush:true)

        /*init market*/
        new Market(serialNumber : 'MKT100', name : '江桥批发市场', region : region_jq, marketType : mt_other, active : true).save(flush:true)
        new Market(serialNumber : 'MKT200', name : '陇上批发市场', region : region_fz, marketType : mt_other, active : true).save(flush:true)
        new Market(serialNumber : 'MKT300', name : '封浜菜场', region : region_jq, marketType : mt_retailer, active : true).save(flush:true)

        /*init department*/
        //commerce
        def department_commerce = 
            new Department(serialNumber:1000, name:'总部', type : 'C', parentDept : null, description:'L1',isActive:true).save(flush:true)
        //region jq
        def department_region_jq = 
            new Department(serialNumber:2000, name:'江桥（大区）', type : 'R', parentDept : department_commerce, description:'L2',isActive:true).save(flush:true)
     
        /*init retailer*/ 
        def retailer_sh = new Retailer(serialNumber:2100, name:'沙河（门店）', department : department_region_jq, region : region_jq, isActive:true).save(flush:true)
        //branch jq jf
        def retailer_jf = new Retailer(serialNumber:2200, name:'江丰（门店）', department : department_region_jq, region : region_jq, isActive:true).save(flush:true)
 
        def retailer_tf = new Retailer(serialNumber:2300, name:'天赋（门店）', department : department_region_jq, region : region_jq, isActive:true).save(flush:true)
 
        def retailer_hh = new Retailer(serialNumber:2400, name:'黄家花园（门店）', department : department_region_jq, region : region_jq, isActive:true).save(flush:true)
        
        /* init admin */
            //profile
            def profile_admin = new Profile(name:"Admin Profile").save(flush:true)
            //create user
            def user_admin = new User(username: "admin",  passwordHash: new Sha512Hash("admin").toHex(), profile : profile_admin).save(flush:true)
            //create role
            def roleAdmin = new Role(name:"Admin Role")
                .addToPermissions("*:*")
                .addToUsers(user_admin)
                .save(flush:true)


        /*init staff*/
        //profile
        def profile_commerce = new Profile(name:"Commerce Profile").save(flush:true)
        def profile_region = new Profile(name:"Region Profile").save(flush:true)
        def profile_branch = new Profile(name:"Branch Profile").save(flush:true)

        //user commerce
        def user_1000_1 = 
            new User(username: "1000_1",  passwordHash: new Sha512Hash("1000_1").toHex(), profile:profile_commerce, department : department_commerce).save(flush:true)        
        //user region jq
        def user_2000_1 = 
            new User(username: "2000_1",  passwordHash: new Sha512Hash("2000_1").toHex(), profile:profile_region, department : department_region_jq).save(flush:true)
        /*
        //user branch
        def user_2100_1 = 
            new User(username: "2100_1",  passwordHash: new Sha512Hash("2100_1").toHex(), profile:profile_branch, department : department_branch_sh).save(flush:true)
        //user
        def user_2200_1 = 
            new User(username: "2200_1",  passwordHash: new Sha512Hash("2200_1").toHex(), profile:profile_branch, department : department_branch_jf).save(flush:true)
        //user
        def user_2300_1 = 
            new User(username: "2300_1",  passwordHash: new Sha512Hash("2300_1").toHex(), profile:profile_branch, department : department_branch_tf).save(flush:true)
        //user
        def user_2400_1 = 
            new User(username: "2400_1",  passwordHash: new Sha512Hash("2400_1").toHex(), profile:profile_branch, department : department_branch_hh).save(flush:true)
        */
        /*staff*/
        def staff_10000 = new Staff(serialNumber : '10000', name :'杨', department : department_commerce, isActive: true).save(flush:true)
        def staff_10001 = new Staff(serialNumber : '10001', name :'庄', department : department_commerce, isActive: true).save(flush:true)
        def staff_10002 = new Staff(serialNumber : '10002', name :'陆', department : department_commerce, isActive: true).save(flush:true)

        /*category*/
        def recordType_category_11 = new RecordType (serialNumber : '1', name : '会计', domain : 'category',  isActive : true, description : '').save(flush:true)
        def recordType_category_12 = new RecordType (serialNumber : '2', name : '产品', domain : 'category',  isActive : true, description : '').save(flush:true)
        
        def category11_01 = new Category (recordType : recordType_category_11, serialNumber : '01', name : '自产', isActive:true).save(flush:true)
        def category11_02 = new Category (recordType : recordType_category_11, serialNumber : '02', name : '外购', isActive:true).save(flush:true)

        def category12_01 = new Category (recordType : recordType_category_12, serialNumber : '01', name : '叶菜', isActive:true).save(flush:true)
        def category12_02 = new Category (recordType : recordType_category_12, serialNumber : '02', name : '叶茎', isActive:true).save(flush:true)
        def category12_03 = new Category (recordType : recordType_category_12, serialNumber : '03', name : '绿叶', isActive:true).save(flush:true)
        def category12_04 = new Category (recordType : recordType_category_12, serialNumber : '04', name : '白叶', isActive:true).save(flush:true)
        /*init warehouse*/
        def warehouse_normal = new Warehouse(serialNumber : '100000', name : '常温仓库', isActive : true).save(flush:true)
        def warehouse_cool = new Warehouse(serialNumber : '100001', name : '冷藏仓库', isActive : true).save(flush:true)
 
        /*product*/
        def product_lct = new Product(serialNumber : '1000001', name : '腊菜苔', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true) 
        def product_xc  = new Product(serialNumber : '1000002', name : '香菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_xtc = new Product(serialNumber : '1000003', name : '小汤菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_hwc = new Product(serialNumber : '1000004', name : '华王菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_bc  = new Product(serialNumber : '1000005', name : '菠菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_jc  = new Product(serialNumber : '1000006', name : '荠菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_phc = new Product(serialNumber : '1000007', name : '蓬蒿菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_sc  = new Product(serialNumber : '1000008', name : '生菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_jsj = new Product(serialNumber : '1000009', name : '金丝菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_qc  = new Product(serialNumber : '1000010', name : '芹菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_hlb = new Product(serialNumber : '1000011', name : '胡萝卜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_02))
            .save(flush:true)
        def product_blb = new Product(serialNumber : '1000012', name : '白萝卜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_02))
            .save(flush:true)
        def product_tc  = new Product(serialNumber : '1000013', name : '塌菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_hyc = new Product(serialNumber : '1000014', name : '黄芽菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_ds  = new Product(serialNumber : '1000015', name : '大蒜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_jxc = new Product(serialNumber : '1000016', name : '卷心菜', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        def product_ws  = new Product(serialNumber : '1000017', name : '莴笋', unit : '斤', shelfLife : 3, isActive : true)
            .addToCategories(new ProductCategory(category : category11_01))
            .addToCategories(new ProductCategory(category : category12_01))
            .addToCategories(new ProductCategory(category : category12_03))
            .save(flush:true)
        /*init dealer*/
        def recordType_supply = new RecordType (serialNumber : '1', name : '供应商', domain : 'dealer',  isActive : true, description : '').save(flush:true)
        def dealer_tx = new Dealer(
            recordType : recordType_supply
            , serialNumber: 'SH0001'
            , name : '天馨庄园'
            , alias : '天馨'
            , manager : 'Leaf'
            , phone : '13818310340'
            , address : '大同路1010弄'
            , active : true
        ).save(flush:true)

        def recordType_earth = new RecordType (serialNumber : '2', name : '地块', domain : 'dealer',  isActive : true, description : '').save(flush:true)
        def dealer_a1 = new Dealer(
            recordType : recordType_earth
            , serialNumber: 'A1'
            , name : 'A1地块'
            , alias : 'A1'
            , manager : 'Leaf'
            , phone : '13818310340'
            , address : '风采'
            , active : true
        ).save(flush:true)
        def dealer_a2 = new Dealer(
            recordType : recordType_earth
            , serialNumber: 'A2'
            , name : 'A2地块'
            , alias : 'A2'
            , manager : 'David'
            , phone : '13818310340'
            , address : '风采'
            , active : true
        ).save(flush:true)
       /*init stock in*/
       
        def recordType_purchase = new RecordType (serialNumber : '1', name : '采购入库', domain : 'stockIn',  isActive : true, description : '').save(flush:true)
        def recordType_produce  = new RecordType (serialNumber : '2', name : '采摘入库', domain : 'stockIn',  isActive : true, description : '').save(flush:true)
        /*
        new StockIn(
              recordType : recordType_purchase
            , dealer : dealer_tx
            , date : new Date()
            , staff : staff_10001
            , status : '草稿'
            , owner : user_1000_1
            , createdBy : user_1000_1
            , lastModifiedBy : user_1000_1
        ).addToDetails(
            new StockInDetail(
                  serialNumber : '01'
                , product : product_lct
                , warehouse : warehouse_normal
                , life : 3
                , unit : '斤'
                , quantity : new BigDecimal(100)
                , surplus : new BigDecimal(100)
                , price : new BigDecimal(1.8)
                , amount : new BigDecimal(180)
                , isOver : false
            )
        ).save(flush:true)
        */

        
        /*
        new StockIn(
              recordType : recordType_produce
            , dealer : dealer_a1
            , date : new Date()
            , staff : staff_10001
            , status : '草稿'
            , owner : user_1000_1
            , createdBy : user_1000_1
            , lastModifiedBy : user_1000_1
        ).addToDetails(
            new StockInDetail(
                  serialNumber : '01'
                , product : product_lct
                , warehouse : warehouse_cool
                , life : 3
                , unit : '斤'
                , quantity : new BigDecimal(100)
                , surplus : new BigDecimal(100)
                , price : new BigDecimal(1.8)
                , amount : new BigDecimal(180)
                , isOver : false
            )
        ).save(flush:true)
        */
        
    }
}
        
